<?php
 class Controller_Index extends Controller_Base_Admin {

     function index($args) {

        $item = new Model_Item($this->id);

        // setting visibility
        if(isset($_GET['visibility'])) 
            $item->save(array(visibility => $_GET['visibility']));

        

        if(isset($_POST['add_partition'])) {
            
            try {
                $new = $item->add($_POST['new_partition'],$_POST['new_alias']);
                
                $site = new Model_Site();
                
                if($site->create[visibility] === '1')
                    $new->save(array(visibility => 1));
                
                if($site->create[access] > 0 ) {
                    error_log(" add access for user: ".$this->admin->id." partition: ". $new->get()->id);
                    $this->admin->saveRule($new->get()->id, $this->admin->id);
                }
                
                
            } catch(ControllerException $e) {
                error_log("ERROR: ".$e->getMessage());

                return new Redirect($e->getLocation());                
                
            }
            error_log(" going to: ".'/admin/'.$_POST['new_partition']);            
            return new Redirect('/admin/'.$_POST['new_partition']);    
                
        }
        
        if(isset($_GET['delete'])) {

            $item->delete();
        
            return new Redirect('/admin/');
        }

        if(isset($_POST['save'])) {
        
            if($_POST['save'] === 'basic') {
                $item->save(array(
                    basic     => 1,
                    alias    => $_POST['alias'],
                    value    => $_POST['value'],
                    text    => stripslashes($_POST['Text']),
                    icon    => $_POST['icon'],
                ));
            } else if($_POST['save'] === 'advanced') {
                
                $item->save(array(
                    advanced    => 1,
                    meta_desc    => $_POST['meta_desc'],
                    meta_keys    => $_POST['meta_keys']
                ));

            }

        }

        $template = new Template('admin/index.tmpl');

        $template->setArgs(
            array(
                'title'     => $item->get()->value, //'авторизация',
                'tabs'         => $this->tabs->get('common'), //,'perview'
                'menu'        => $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
                'content'    => $item->get(),
                'text_size' => strlen($item->get()->text),
                'text_percent'  => round(strlen($item->get()->text) / 65000 * 100),
                '_user'        => $this->admin,
            )
        );

        return $template;

     } 
 }
 
?>
